import 'package:flutter/material.dart';
import 'package:flutter_menu/res/app_color.dart';
import 'package:flutter_menu/res/app_style.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

//规格选项组件
class OptionItem extends StatelessWidget {
  const OptionItem({
    super.key,
    required this.label,
    this.subLabel,
    this.isSelected = false,
    this.isEnable = true,
    this.isIntrinsicWidth = true,
    this.onTap,
    this.maxLines,
    this.overflow,
  });

  final String label; // 中间文字
  final String? subLabel; // 补充文字
  final int? maxLines; // maxLines for label
  final TextOverflow? overflow;
  final bool isSelected; //已选择，未选择
  final bool isEnable; //是否可以点击
  final bool isIntrinsicWidth; // true: 普通规格按钮, 固定最小宽度， false： 套餐按钮，宽度自适应
  final VoidCallback? onTap; // 点击事件

  Color get bgColor {
    return isEnable
        ? (isSelected
            ? AppColors.buttonIsSelected
            : AppColors.buttonIsUnSelected)
        : AppColors.buttonDisable;
  }

  @override
  Widget build(BuildContext context) {
    return Opacity(
      opacity: isEnable ? 1 : 0.5,
      child: Material(
        child: Ink(
          decoration: BoxDecoration(
            color: bgColor,
            borderRadius: BorderRadius.circular(12),
          ),
          child: InkWell(
            onTap: isEnable ? onTap : null,
            borderRadius: BorderRadius.circular(12),
            highlightColor: AppColors.itemHighLight, // 背景颜色
            child: setIntrinsicWidthContainer(
                child: Container(
                  padding:
                      EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.w),
                  alignment: Alignment.center,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        label,
                        maxLines: maxLines,
                        overflow: overflow,
                        style: TextStyle(
                          fontSize: 25.sp,
                          color: isEnable
                              ? AppColors.textInBottomSheet
                              : AppColors.text_646464,
                        ),
                      ),
                      subLabel != null
                          ? Text(
                              subLabel!,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 19.sp,
                                color: isEnable
                                    ? AppColors.text_915D44
                                    : AppColors.text_646464,
                              ),
                            )
                          : const SizedBox.shrink()
                    ],
                  ),
                ),
                isIntrinsicWidth: isIntrinsicWidth),
          ),
        ),
      ),
    );
  }

  Widget setIntrinsicWidthContainer({
    required Widget child,
    bool isIntrinsicWidth = true,
  }) {
    if (!isIntrinsicWidth) {
      return child;
    }
    return IntrinsicWidth(
      child: ConstrainedBox(
        constraints: BoxConstraints(minWidth: 117.w, minHeight: 62.w),
        child: child,
      ),
    );
  }
}
